
package org.apache.solr.logging.jul;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.solr.logging.LoggerInfo;

public class JulInfo extends LoggerInfo {

    private static final Level[] LEVELS = {
        null, // aka unset
        Level.FINEST,
        Level.FINE,
        Level.CONFIG,
        Level.INFO,
        Level.WARNING,
        Level.SEVERE,
        Level.OFF
    };

    final Logger logger;

    public JulInfo(String name, Logger logger) {
        super(name);
        this.logger = logger;
    }

    @Override
    public String getLevel() {
        if(logger == null) {
            return null;
        }

        Level llevel = logger.getLevel();
        if(llevel != null) {
            return llevel.getName();
        }
        for(Level l : LEVELS) {
            if(l == null) {
                // avoid NPE
                continue;
            }
            if(logger.isLoggable(l)) {
                // return first level loggable
                return l.getName();
            }
        }
        return Level.OFF.getName();
    }

    @Override
    public boolean isSet() {

        return (logger != null && logger.getLevel() != null);
    }
}